package mao.leetcode.offer;

/**
 * @author Totoro
 * @date 2020/6/15
 * @explain:
 */
public class interview19 {


        char[] charS;
        char[] charP;
        int lenS,lenP;
        public boolean isMatch(String s, String p) {
            charS=s.toCharArray();
            charP=p.toCharArray();
            lenS=charS.length;
            lenP=charP.length;
            return helper(0,0);
        }

        public boolean helper( int indS,int indP){
            if(indS == charS.length && indP == charP.length){
                return true;
            }
            //先进行这个判断，防止出现 s:ab  p:adc* 的情况。
            if (indP+1<charP.length && charP[indP+1] == '*' ){
                if (indS<charS.length && (charP[indP] == charS[indS] || charP[indP] == '.')){
                    return helper(indS+1,indP)||  //匹配当前字符，状态不变
                            helper(indS+1,indP+2) ||  //匹配当前字符，状态改变
                            helper(indS,indP+2);      //不匹配当前字符
                }else{
                    return helper(indS,indP+2);
                }
            }

            if(indS >= charS.length || indP >= charP.length){
                return false;
            }

            if (charS[indS] == charP[indP] || charP[indP] == '.'){
                return helper(indS+1,indP+1);
            }else{
                return false;
            }
        }

}
